<?php

namespace app\platform\controller\terminal;

use app\BaseController;

use app\common\logic\DataLogic;

use app\common\model\ShopPosterModel;



class ShopPoster extends BaseController{

    public function index()

    {

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0]

        ];

        $shop_poster= new ShopPosterModel();

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['add_time_format'];

        $return=DataLogic::getDataList($shop_poster,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }



    public function show()

    {

        $poster_template_id=$this->request->param('poster_template_id/d',0);

        if($poster_template_id==0) return $this->success([]);



        if(null === $poster = ShopPosterModel::find($poster_template_id)) return $this->error('海报不存在');

        if($poster->shop_id != $this->request->shopId) return $this->error('海报不存在');

        if($poster->is_delete != 0) return $this->error('海报不存在');



        $detail=[

            'copywriting'=>$poster->copywriting,

            'back_img'=>$poster->back_img,

            'qrcode_info'=>$poster->qrcode_info

        ];

        return $this->success(['detail'=>$detail]);

    }



    public function save()

    {

        $poster_template_id=$this->request->param('poster_template_id/d',0);

        $copywriting=$this->request->param('copywriting');

        $back_img=$this->request->param('back_img');

        $qrcode_info=$this->request->param('qrcode_info/a',[]);

        if(empty($copywriting)) return $this->error('请输入海报文案');

        if(empty($back_img)) return $this->error('请上传背景图');

        if(empty($qrcode_info) || !is_array($qrcode_info)) return $this->error('没有二维码信息#1');

        if(count($qrcode_info) != 4) return $this->error('没有二维码信息#2');

        if(!isset($qrcode_info['width']) || $qrcode_info['width']<=0 || $qrcode_info['width']>375) return $this->error('二维码宽度不能小于等于0，且不能大于375');

        if(!isset($qrcode_info['height']) || $qrcode_info['height']<=0 || $qrcode_info['height']>680) return $this->error('二维码宽度不能小于等于0，且不能大于680');

        if(!isset($qrcode_info['x']) || $qrcode_info['x'] <0 || $qrcode_info['x']>(375-$qrcode_info['width'])) return $this->error('二维码左侧偏移不能小于0，且不能大于'.(375-$qrcode_info['width']));

        if(!isset($qrcode_info['y']) || $qrcode_info['y'] <0 || $qrcode_info['y']>(680-$qrcode_info['height'])) return $this->error('二维码顶部偏移不能小于0，且不能大于'.(680-$qrcode_info['height']));



        if($poster_template_id != 0){

            if(null === $poster = ShopPosterModel::find($poster_template_id)) return $this->error('海报不存在');

            if($poster->shop_id != $this->request->shopId) return $this->error('海报不存在');

            if($poster->is_delete != 0) return $this->error('海报不存在');



        }else{

            $poster = new ShopPosterModel();

            $poster->shop_id = $this->request->shopId;

        }



        $poster->copywriting=$copywriting;

        $poster->back_img=$back_img;

        $poster->qrcode_info=$qrcode_info;

        $poster->save();



        return $this->success('操作成功');

    }



    public function del()

    {

        $poster_template_id=$this->request->param('poster_template_id/d',0);

        if($poster_template_id==0) return $this->success([]);



        if(null === $poster = ShopPosterModel::find($poster_template_id)) return $this->error('海报不存在');

        if($poster->shop_id != $this->request->shopId) return $this->error('海报不存在');

        if($poster->is_delete != 0) return $this->error('海报不存在');



        $poster->is_delete=1;

        $poster->save();

        return $this->error('删除成功');

    }

}

